<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\AdPrice;
use App\Models\AdCategory;
use App\Models\AdHierarchy;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class AdPriceController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new AdPrice(['category:id,name','hierarchy:id,name']), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('category.name','广告类别');
            $grid->column('type','广告类型')
                 ->using([1 => '图片',2 => '视频'])
                 ->label([1 => 'success',2 => 'danger']);
            $grid->column('hierarchy.name','广告层级');
            $grid->column('price');
            $grid->column('status')
                ->using([1 => '启用',2 => '禁用'])
                ->label([1 => 'success',2 => 'danger']);
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new AdPrice(), function (Show $show) {
            $show->field('id');
            $show->field('ad_category_id');
            $show->field('type');
            $show->field('ad_hierarchy_id');
            $show->field('price');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new AdPrice(), function (Form $form) {
            $form->display('id');
            $form->select('ad_category_id')
                 ->options(AdCategory::getOption())
                 ->default(1)
                 ->required();
            $form->radio('type')
                 ->options([1=> '图片广告',2 => '视频广告'])
                 ->default(1)
                 ->required();
            $form->select('ad_hierarchy_id')
                 ->options(AdHierarchy::getOption())
                 ->required();
            $form->currency('price')
                 ->symbol('￥')
                 ->default(0.01)
                 ->required();
            $form->radio('status')
                ->options([1 => '启用',2 => '禁用'])
                ->default(1)
                ->required();

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
